﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{
    public class orederInfoDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int oInfoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from orederInfo where oInfoId=@oInfoId ");
            htSyn.Add("oInfoId", oInfoId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得订单详细列表
        /// <summary>
        /// 获得订单详细列表
        /// </summary>
        /// <returns></returns>
        public IList<orederInfo> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select oInfoId,productId,orderId,price,productNum,oDate,sendTel,sendAdd from orederInfo ");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据订单详细号查询实体
        /// <summary>
        /// 根据订单详细号查询实体
        /// </summary>
        /// <param name="oInfoId"></param>
        /// <returns></returns>
        public orederInfo GetModel(int oInfoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select oInfoId,productId,orderId,price,productNum,oDate,sendTel,sendAdd from orederInfo ");
            strSql.Append(" where oInfoId=@oInfoId ");
            htSyn.Add("@oInfoId", oInfoId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            orederInfo orederinfo = new orederInfo();
            if (dt.Rows.Count>0)
            {
                orederinfo.oInfoId = Convert.ToInt32(dt.Rows[0]["oInfoId"]);
                orederinfo.productId = Convert.ToInt32(dt.Rows[0]["productId"]);
                orederinfo.orderId = Convert.ToInt32(dt.Rows[0]["orderId"]);
                orederinfo.price = Convert.ToDecimal(dt.Rows[0]["price"]);
                orederinfo.productNum = Convert.ToInt32(dt.Rows[0]["productNum"]);
                orederinfo.oDate = Convert.ToDateTime(dt.Rows[0]["oDate"]);
                orederinfo.sendTel = dt.Rows[0]["sendTel"].ToString();
                orederinfo.sendAdd = dt.Rows[0]["sendAdd"].ToString();
            }           
            return orederinfo;
        }
        #endregion

        #region 修改订单详细信息
        /// <summary>
        /// 修改订单详细信息
        /// </summary>
        /// <param name="orederInfo"></param>
        /// <returns></returns>
        public bool Update(orederInfo orederinfo)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update orederInfo set ");
            strSql.Append("productId=@productId,");
            strSql.Append("orderId=@orderId,");
            strSql.Append("price=@price,");
            strSql.Append("productNum=@productNum,");
            strSql.Append("oDate=@oDate,");
            strSql.Append("sendTel=@sendTel,");
            strSql.Append("sendAdd=@sendAdd");
            strSql.Append(" where oInfoId=@oInfoId ");
            htSyn.Add("@oInfoId",orederinfo.oInfoId);
            htSyn.Add("@productId",orederinfo.productId);
            htSyn.Add("@orderId",orederinfo.orderId);
            htSyn.Add("@price",orederinfo.price);
            htSyn.Add("@productNum",orederinfo.productNum);
            htSyn.Add("@oDate",orederinfo.oDate);
            htSyn.Add("@sendTel",orederinfo.sendTel);
            htSyn.Add("@sendAdd", orederinfo.sendAdd);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除订单详细根据订单详细号
        /// <summary>
        /// 删除订单详细根据订单详细号
        /// </summary>
        /// <param name="oInfoId"></param>
        /// <returns></returns>
        public bool Delete(int oInfoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from orederInfo where oInfoId=@oInfoId");
            htSyn.Add("oInfoId", oInfoId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加订单详细
        /// <summary>
        /// 添加订单详细
        /// </summary>
        /// <param name="orederInfo"></param>
        /// <returns></returns>
        public bool Insert(orederInfo orederinfo)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into orederInfo([productId],[orderId],[price],[productNum],[oDate],[sendTel],[sendAdd]) values (@productId,@orderId,@price,@productNum,@oDate,@sendTel,@sendAdd)");
            htSyn.Add("@productId", orederinfo.productId);
            htSyn.Add("@orderId", orederinfo.orderId);
            htSyn.Add("@price", orederinfo.price);
            htSyn.Add("@productNum", orederinfo.productNum);
            htSyn.Add("@oDate", orederinfo.oDate);
            htSyn.Add("@sendTel", orederinfo.sendTel);
            htSyn.Add("@sendAdd", orederinfo.sendAdd);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<orederInfo> getProductsBySql(string strSql)
        {
            IList<orederInfo> list = new List<orederInfo>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    orederInfo orederinfo = new orederInfo();
                    orederinfo.oInfoId = Convert.ToInt32(dr["oInfoId"]);
                    orederinfo.productId = Convert.ToInt32(dr["productId"]);
                    orederinfo.orderId = Convert.ToInt32(dr["orderId"]);
                    orederinfo.price = Convert.ToDecimal(dr["price"]);
                    orederinfo.productNum = Convert.ToInt32(dr["productNum"]);
                    orederinfo.oDate = Convert.ToDateTime(dr["oDate"]);
                    orederinfo.sendTel = dr["sendTel"].ToString();
                    orederinfo.sendAdd = dr["sendAdd"].ToString();
                    list.Add(orederinfo);
                }
            }
            return list;
        }
        #endregion
    }
}
